package util

import (
	"crypto/tls"
	"math/rand"
	"net/smtp"
	"time"

	"github.com/jordan-wright/email"
)

func RandomString(n int) string {
	var letters = []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")
	result := make([]byte, n)

	rand.Seed(time.Now().Unix())
	for i := range result {
		result[i] = letters[rand.Intn(len(letters))]
	}
	return string(result)
}

func SendVcodeMail(toEmail string, vcode string) error {
	// 发件人信息
	mailUsername := "1981888213@qq.com" // 邮箱账号
	mailPassword := "epogbzxhnoiicheg"  // 邮箱授权码
	addr := "smtp.qq.com:465"           // TLS地址
	host := "smtp.qq.com"               // 邮件服务器地址

	// 邮件信息
	e := email.NewEmail()
	e.From = "eQuantum <1981888213@qq.com>"
	e.To = []string{toEmail}
	e.Subject = "eQuantum身份验证"
	e.HTML = []byte("您好！您的验证码是：<h1>" + vcode + "</h1>")

	// 发送邮件
	err := e.SendWithTLS(addr, smtp.PlainAuth("", mailUsername, mailPassword, host),
		&tls.Config{InsecureSkipVerify: true, ServerName: host})

	return err
}
